package service

import (
	"context"
	"go-micro/src/Course"
	"go-micro/src/Mapper"
)

func NewCourseModel(id int32, name string) *Course.CourseModel {
	return &Course.CourseModel{CourseId: id, CourseName: name}
}

type CourseServiceImpl struct{}

func NewCourseServiceImpl() *CourseServiceImpl {
	return &CourseServiceImpl{}
}

func (g *CourseServiceImpl) ListForTop(ctx context.Context, req *Course.ListRequest, rsp *Course.ListResponse) error {
	course := make([]*Course.CourseModel, 0)
	err := Mapper.GetCourseList(&course)
	if err != nil {
		return err
	}
	rsp.Result = course
	return nil
}

func (g *CourseServiceImpl) CourseDetail(ctx context.Context, req *Course.CourseDetailRequest, rsp *Course.DetailResponse) error {

	//取详情
	if req.FetchType == 1 || req.FetchType == 3 {
		course := Mapper.GetCourseDetail(int(req.Cid))
		rsp.CourseDetail = course
	}
	//取count
	if req.FetchType == 2 || req.FetchType == 3 {
		count := Mapper.GetCourseCount(int(req.Cid))
		rsp.CourseCount = count

	}

	return nil
}
